package it.interazionemultimodale.twitterrecognizer.utility;

import it.interazionemultimodale.twitterrecognizer.twitterutility.TweetInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.widget.Toast;

public class Methods
{
	private Methods()
	{
	}
	
	
	//Returns true on string found into the array
	public static boolean stringInArray(String stringSearched, List<String> list)
	{
		for (String string : list)
			if (string.compareToIgnoreCase(stringSearched) == 0)
				return true;
		
		return false;
	}
	
	
	//Returns true on network connection available and connected or connecting
	public static boolean checkInternetConnection(Context context)
	{
		ConnectivityManager connectionManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = connectionManager.getActiveNetworkInfo();
		return netInfo != null && netInfo.isAvailable() && netInfo.isConnectedOrConnecting();
	}
	
	
	//Returns true on speech recognition system available
	public static boolean checkSpeechRecognition(Context context)
	{
		PackageManager pm = context.getPackageManager();
		List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
		
		return activities.size() != 0;
	}
	
	//Shows a standard toast with the text and into the context specified
	public static void showToast(Context context, String message)
	{
		Toast.makeText(context, message, Toast.LENGTH_LONG).show();
	}
	
	
	//Returns a SpeechRecognizer intent
	public static Intent getSpeechRecognizerIntent()
	{
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
	    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
	    return intent;
	}
	
	
	//Speaks aloud informations of a TweatInfo object
	public static void readTweetAloud(TextToSpeech tts, TweetInfo tweetInfo)
	{
		//Try to set status user's language to TextToSpeech system
		int error = tts.setLanguage(new Locale(tweetInfo.getUserLang()));
		if (error == TextToSpeech.LANG_MISSING_DATA || error == TextToSpeech.LANG_NOT_SUPPORTED)
			System.out.println("Errore: lingua di sistema non supportata");
		
		tts.speak("Tuit di " + tweetInfo.getUsername(), TextToSpeech.QUEUE_ADD, null);
		tts.playSilence(700, TextToSpeech.QUEUE_ADD, null);
		tts.speak(tweetInfo.getSimplifiedStatusText(), TextToSpeech.QUEUE_ADD, null);
		tts.playSilence(450, TextToSpeech.QUEUE_ADD, null);
		
		tts.setLanguage(Locale.getDefault());
		
		if(tweetInfo.statusTextContainsMedia())
			tts.speak("Contiene foto.", TextToSpeech.QUEUE_ADD, null);
		if(tweetInfo.statusTextContainsURL())
			tts.speak("Contiene link.", TextToSpeech.QUEUE_ADD, null);
	}
	
	
	//Speaks aloud informations of a list of TweetInfo objects
	public static void readTweetsAloud(TextToSpeech tts, HashMap<String, String> ttsHashMap, List<TweetInfo> tweetList)//, Context context)
	{
		int i = 1;
		for (TweetInfo tweetInfo : tweetList)
		{
			ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "tweet/" + i + "/" + tweetList.size());
			tts.speak("Tweet " + (i++) + " di " + tweetList.size(), TextToSpeech.QUEUE_ADD, ttsHashMap);
			readTweetAloud(tts, tweetInfo);
		}
		ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "tweet/" + i + "/" + tweetList.size());
		tts.playSilence(100, TextToSpeech.QUEUE_ADD, ttsHashMap);
	}
}
